import {Component, OnInit} from '@angular/core';
import {CooperationService} from '../../../cooperation/cooperation.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {UsedStorePService} from '../../../used-store/used-store-p.service';
import {AssessPriceService} from '../../assess-price.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-question-manage',
  templateUrl: './question-manage.component.html',
  styleUrls: ['./question-manage.component.scss']
})
export class QuestionManageComponent implements OnInit {
  query = {
    name: '',
    date: null,
    // 当前页
    page: 1,
    // 分页大小
    pageSize: 20,
  };
  listOfData = {
    page: 1,
    total: 100,
    pageSize: 20,
    records: []
  };

  constructor(
    private assessPriceService: AssessPriceService,
    private nzMessageService: NzMessageService,
    private router: Router,
  ) {
  }

  ngOnInit(): void {
    this.queryList();
  }

  queryBtn() {
    this.query.page = 1;
    this.queryList();
  }

  resetQuery() {
    this.query = {
      name: '',
      date: null,
      page: 1,
      pageSize: 20,
    };
    this.queryList();
  }

  // 查询列表
  queryList(): void {
    // @ts-ignore
    const start = this.query.date ? this.assessPriceService.formatTime(this.query.date[0], 'date') : '';
    // @ts-ignore
    const end = this.query.date ? this.assessPriceService.formatTime(this.query.date[1], 'date') : '';
    this.assessPriceService.getQuestionList(
      this.query.page,
      this.query.pageSize,
      this.query.name,
      start,
      end,
    ).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data.list;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.queryList();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.queryList();
  }

  // 删除
  deleteRow(id: any) {
    this.assessPriceService.updateQuestion({id, deleteState: 1}, []).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryList();
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  add() {
    this.router.navigate(['/aisi-recyc/assessPriceQuestionEdit']);
  }

  edit(id: any) {
    this.router.navigate(['/aisi-recyc/assessPriceQuestionEdit'], {queryParams: {id, type: 'edit'}});
  }

  copy(id: any) {
    this.router.navigate(['/aisi-recyc/assessPriceQuestionEdit'], {queryParams: {id, type: 'copy'}});
  }
}
